package com.wxzz.elearing.course.quartz;

import com.wxzz.elearing.course.service.IStudyPlanService;
import com.wxzz.elearning.common.constants.RedisConstants;
import com.wxzz.elearning.common.utils.IPUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Component
public class StudyPlanQuartz {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 每4小时执行一次
     * 统计一次学习计划完成情况
     */
    @Scheduled(cron = "0 0 */4 * * ?")
   // @Scheduled(cron = "*/10 * * * * ?")
    public void studyPlanFinish() {
        //分布式环境下进行幂等
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(RedisConstants.STUDY_PLAN_QUARTZ, RedisConstants.STUDY_PLAN_QUARTZ, 60 * 60, TimeUnit.SECONDS);
        if (!Boolean.TRUE.equals(aBoolean)) {
            //如果已存在则直接退出任务
            return;
        }
        studyPlanService.updateStudyPlanFinish();
    }

    @Autowired
    private IStudyPlanService studyPlanService;
}
